// pages/ucenter/transferCredit/transferCredit.js

var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: '',
    users: [],
    open: false,
    number: '',
    userId: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  bindExchange: function (e) {
    this.setData({
      phoneNum: e.detail.value,
      users: []
    });
  },
  bindExchangeNumber: function (e) {
    this.setData({
      number: e.detail.value
    });
  },
  clearExchange: function () {
    this.setData({
      phoneNum: '',
      users: [],
      userId: -1
    });
  },
  closeAttr: function () {
    this.setData({
      open: false,
      userId: -1,
      number: ''
    });
  },
  goClick: function(e) {
    this.setData({
      open: true,
      userId: e.currentTarget.dataset.id
    })
  },
  submit: function() {
    console.log(this.data.number)
    if (this.data.number.length===0 || this.data.number<=0) {
      util.showErrorToast("请输入积分");
      return;
    }
    let that = this
    util.request(api.CreditTransfer, {
      transferUserId: that.data.userId,
      credit: parseFloat(that.data.number)
    }, 'GET').then(function (res) {
      if (res.errno === 0) {
        console.log(res.data)
        util.showSuccessToast("转入成功");
        that.setData({
          open: false,
        });
      }
      else {
        util.showErrorToast(res.errmsg);
        that.setData({
          open: false,
        });
      }
    });
  },
  goSelectUser: function () {
    if (this.data.phoneNum.length === 0) {
      util.showErrorToast("请输入手机号");
      return;
    }

    let that = this;
    util.request(api.UserPhone, {
      phone: that.data.phoneNum
    }, 'GET').then(function (res) {
      if (res.errno === 0) {
        console.log(res.data)
        if(res.data.length>0) {
          that.setData({
            users: res.data,
          });
        }else {
          util.showErrorToast("未查到用户");
        }
       
      }
      else {
        util.showErrorToast(res.errmsg);
      }
    });
  },
})